package com.wzt.lianfirecontrol.fragment.message;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.message.MessageListModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.Utils;

import java.util.HashMap;

public class MessageDetailFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private TextView tv_title;
    private TextView tv_date;
    private TextView tv_content;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private static final int MESSAGE_DETAIL_REQUEST_WHAT = 0;

    private MessageListModel messageListModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            activity = getMyActivity();
            bundle = getArguments();
            if (null != bundle){
                messageListModel = (MessageListModel) bundle.getSerializable("data");
            }
            if (null == messageListModel){
                activity.finish();
            }
            itemContentView = inflater.inflate(R.layout.f_message_detail, container, false);
            initView();
            setDetailData(messageListModel);
            initHttpHelper();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        tv_title = itemContentView.findViewById(R.id.tv_title);
        tv_date = itemContentView.findViewById(R.id.tv_date);
        tv_content = itemContentView.findViewById(R.id.tv_content);

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        include_progress_bar.setVisibility(View.GONE);

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_no_data.setVisibility(View.GONE);

        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setVisibility(View.GONE);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    /**
     * 获取列表
     */
    private void initHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> url_params = new HashMap<>();
        url_params.put("id", messageListModel.getId());
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(url_params));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.MESSAGE_DETAIL_URL, MESSAGE_DETAIL_REQUEST_WHAT, handler, true, hashMap);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }


    private class InfoListListHttpHelper extends HttpHelper {

        public InfoListListHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }
    }

    protected class InfoListParseJsonData extends ParseJsonData{

        public InfoListParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            switch (what){
                case MESSAGE_DETAIL_REQUEST_WHAT:
                    break;
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            //ToastUtils.showToast(activity, bundle.getString(ParseJsonData.ERROR_MSG));
            if (msg.what == MESSAGE_DETAIL_REQUEST_WHAT) {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }
        switch (msg.what){
            case MESSAGE_DETAIL_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                break;
        }
    }

    private void setDetailData(MessageListModel messageDetail) {
        if (messageDetail == null){
            include_no_data.setVisibility(View.VISIBLE);
        }else {
            include_no_data.setVisibility(View.GONE);
            tv_title.setText(messageDetail.getTitle());
            tv_date.setText(messageDetail.getCreateTime().replace("T", ""));
            tv_content.setText(messageDetail.getContent());
        }
    }
}
